// pages/bootstrap/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  onLoad: function (options) {
    if (options.q) {
      this.query = options.q;
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var self = this;


    try {
      var value = wx.getStorageSync('location')
      if (value) {
        app.location = value;
      } else {
        wx.getLocation({
          type: 'gcj02',
          success(res) {
            var location = {};
            location.lat = res.latitude
            location.lng = res.longitude
            wx.setStorageSync("location", location);
            app.location = location;
          }
        });
      }
    } catch (e) {
      // Do something when catch error
    }


    wx.login({
      success(res) {
        console.info(res);
        if (res.code) {
          var tempParams = {};
          tempParams.type = app.tenantKey;
          tempParams.code = res.code;
          if (app.tenant) {
            tempParams.tenant = app.tenant;
          }
          app.postData(app.url + "/rest/saas_user/loginOauth.htm", tempParams, self.backLogin);
        }
      }

    });
  },
  backLogin(res) {
    if (res.code == 0) {
      if (res.userToken) {
        app.userToken = res.userToken;
      }
      if (res.openId) {
        app.openId = res.openId;
      }
      if (this.query) {
       
        var query = decodeURIComponent(this.query);
        console.info(query);
        if (query.indexOf("dishcoupon") > -1) {
          var index = query.indexOf("id=");
          if(index>-1){
            var id=query.substring(index+3);
            wx.redirectTo({
              url: '/pages/coupon/promote/index?id='+id,
            })
            return;
          }
        }

      }



      if (res.navUrl) {
        wx.redirectTo({
          url: res.navUrl,
        })
      } else {
        wx.switchTab({
          url: '/pages/main/home/index',
        })
      }

    } else {
      wx.switchTab({
        url: '/pages/main/home/index',
      })
    }
  }
})